package com.keguoyu.easymvp.ioc;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.keguoyu.easymvp.annotation.ForInvoker;
import com.keguoyu.easymvp.commons.PluginFactory;

public class SingletonConfig {
  public static final String INVOKER_ID = "SINGLETON_REG";
  private static final Map<Class<?>, Collection<ImplMapper>> mappers = new HashMap<>();

  private SingletonConfig() {
    throw new RuntimeException("can't create SingletonConfig instance");
  }

  public static Map<Class<?>, Collection<ImplMapper>> getMappingMap() {
    doRegister();
    return mappers;
  }

  @ForInvoker(methodId = INVOKER_ID)
  private static void doRegister(){
  }

  public static <T> void register(Class<T> cls, PluginFactory<T> factory) {
    Collection<ImplMapper> implMappers = mappers.get(cls);
    if (implMappers == null) {
      implMappers = new HashSet<>();
      mappers.put(cls, implMappers);
    }
    implMappers.add(new ImplMapper(cls, factory, 1));
  }
}
